<template>
    <el-select
        v-model="selectedVal"
        :clearable="clearable"
        class="filter-item"
        :collapse-tags="multiple"
        :multiple="multiple"
        filterable
        :placeholder="'请选择' + (multiple ? ', 可多选' : '')"
        :style="multiple ? 'width: 220px;' : 'width: 150px;'">
        <el-option label="请选择" :value="0" v-if="form"></el-option>
        <el-option
                v-for="(item, index) in easyConfigs"
                :key="index"
                :label="item.id + ' ) ' + item.name"
                :value="item.id">
        </el-option>
    </el-select>
</template>

<script>
    export default {
        props: {
            value: {
                type: [String, Number, Array],
            },
            // 多选
            multiple: {
                type: Boolean,
                default: false,
            },
            clearable: {
                type: Boolean,
                default: true,
            },
            form: {
                type: Boolean,
                default: false,
            },
            type: {
                type: [String, Number, Array],
                default: null,
            },
        },
        data () {
            return {
                selectedVal: '',
            };
        },
        computed: {
            easyConfigs () {
                if (!this.type) return this.$store.state.easyConfig.all;
                let types = this.type;
                if (!Array.isArray(types)) types = [types];
                return this.$store.state.easyConfig.all.filter(v => {
                    return types.includes(v.key);
                });
            },
        },
        watch: {
            value() {
                this.syncForm();
            },
            selectedVal() {
                this.$emit('input', this.selectedVal);
            }
        },
        methods: {
            getAll() {
                if (!this.easyConfigs.length) this.$store.dispatch('easyConfig/All', 'easyConfig');
            },
            syncForm() {
                this.selectedVal = this.value || (this.form ? 0 : '');
            },
        },
        created() {
            this.getAll();
            this.syncForm();
        }
    }
</script>

<style scoped>

</style>
